<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

class Source extends Backend
{
    protected $model = null;
    public function __construct()
    {
        parent::__construct();
        $this->model = model("Business.Source");
    }
    // 查看
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            //返回json数据
            $result = ['total' => $total, 'rows' => $list];

            return json($result);
        }
        return $this->view->fetch();
    }
    // 添加
    public function add()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param('row/a');
            $result = $this->model->save($params);
            if ($result === FALSE) {
                $this->error($this->model->getError());
                exit;
            } else {
                $this->success();
                exit;
            }
        }
        return $this->view->fetch();
    }
    // 编辑
    public function edit($ids = null)
    {
        $ids = $this->request->param('ids');
        $row = $this->model->find($ids);
        if (!$row) {
            $this->error(__('No results were found'));
            exit;
        }
        if ($this->request->isAjax()) {
            $params = $this->request->param('row/a');
            $params['id'] = $ids;
            $result = $this->model->isUpdate(true)->save($params);
            if ($result === FALSE) {
                $this->error($this->model->getError());
                exit;
            } else {
                $this->success();
                exit;
            }
        }
        $this->assign([
            'row' => $row,
        ]);
        return $this->view->fetch();
    }
    // 删除
    public function del($ids = null)
    {
        $ids = $this->request->param('ids');
        $row = $this->model->find($ids);
        if (!$row) {
            $this->error(__('No results were found'));
            exit;
        }
        $result = $this->model->destroy($ids);
        if ($result === FALSE) {
            $this->error($this->model->getError());
            exit;
        } else {
            $this->success();
            exit;
        }
    }
}